const initState = {
  auth: {} as Auth,
  profile: {} as Profile,
  user: {} as User,
};

// token 类型
export type Auth = {
  token: string;
  refresh_token: string;
};

// 用户信息 类型
export type User = {
  id: string;
  name: string;
  photo: string;
  art_count: number;
  follow_count: number;
  fans_count: number;
  like_count: number;
};

// 用户资料 类型
export type Profile = {
  id: string;
  photo: string;
  name: string;
  mobile: string;
  gender: number;
  birthday: string;
  intro: string;
};

export type UserAction =
  | { type: "user/saveAuth"; payload: Auth }
  | { type: "user/clearAuth" }
  | { type: "user/saveUser"; payload: User }
  | { type: "user/saveProfile"; payload: Profile };

// 修改 默认的导出
export default function userReducer(state = initState, action: UserAction) {
  switch (action.type) {
    case "user/saveAuth":
      return {
        ...state,
        auth: action.payload,
      };
    case "user/clearAuth":
      return {
        ...state,
        auth: {} as Auth,
      };
    case "user/saveUser":
      return {
        ...state,
        user: action.payload,
      };
    case "user/saveProfile":
      return {
        ...state,
        profile: action.payload,
      };
    default:
      return state;
  }
}
